package com.ruoyi.web.controller.api.webapp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.house.domain.MyFavorite;
import com.ruoyi.house.service.IMyFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 我的收藏Controller
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/api/webapp/house/favorite")
public class WAppMyFavoriteController extends BaseController {
    @Autowired
    private IMyFavoriteService myFavoriteService;

    /**
     * 查询我的收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MyFavorite myFavorite) {
        startPage();
        List<MyFavorite> list = myFavoriteService.selectMyFavoriteList(myFavorite);
        return getDataTable(list);
    }

    /**
     * 获取我的收藏详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(myFavoriteService.selectMyFavoriteById(id));
    }

    /**
     * 新增我的收藏
     */
    @PostMapping
    public AjaxResult add(@RequestBody MyFavorite myFavorite) {
        if (CollectionUtils.isEmpty(myFavoriteService.selectMyFavoriteList(myFavorite))) {
            return toAjax(myFavoriteService.insertMyFavorite(myFavorite));
        }
        return error("请勿重复收藏");
    }


    /**
     * 删除我的收藏
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(myFavoriteService.deleteMyFavoriteByIds(ids));
    }


    /**
     * 通过用户id 和 房源id
     * 删除我的收藏
     */
    @DeleteMapping("/{userId}/{houseZuInfoId}")
    public AjaxResult removeByUserIdAndHouseZuInfoId(@PathVariable Long userId, @PathVariable Long houseZuInfoId) {
        return toAjax(myFavoriteService.deleteMyFavoriteByUserIdAndHouseZuInfoId(userId, houseZuInfoId));
    }
}
